package com.lcb.rabbitmq.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcb.rabbitmq.base.BaseApiService;
import com.lcb.rabbitmq.base.BaseResponse;
import com.lcb.rabbitmq.entity.Mail;
import com.lcb.rabbitmq.entity.MsgLog;
import com.lcb.rabbitmq.mapper.MsgLogMapper;
import com.lcb.rabbitmq.service.MsgLogService;
import com.lcb.rabbitmq.utils.JodaTimeUtil;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

/****
 * @Author:lcb
 * @Description:MsgLog业务层接口实现类
 * @Date: 2021-01-27 10:00:03
 *****/
@RestController
public class MsgLogServiceImpl extends BaseApiService implements MsgLogService {


    @Autowired
    private MsgLogMapper msgLogMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/sendMail")
    public BaseResponse sendMail(@RequestBody Mail mail) {

        String msgId = UUID.randomUUID().toString();
        mail.setMsgId(msgId);

        String jsonString = JSON.toJSONString(mail);
        MsgLog msgLog = new MsgLog();
        msgLog.setMsgId(msgId);
        msgLog.setExchange("mail.exchange");
        msgLog.setRoutingKey("mail.routing.key");
        Date date = new Date();
        msgLog.setCreateTime(date);
        msgLog.setUpdateTime(date);
        msgLog.setNextTryTime(JodaTimeUtil.plusMinutes(date, 1));
        msgLog.setTryCount(0);
        msgLog.setStatus(0);
        msgLog.setMsg(jsonString);

        //消息入库
        msgLogMapper.insert(msgLog);
        CorrelationData correlationData = new CorrelationData(msgId);

        rabbitTemplate.convertAndSend("mail.exchange", "mail.routing.key", jsonString, correlationData);

        return setResultSuccess("发送邮件成功!");
    }

    /**
     * MsgLog条件+分页查询
     *
     * @param msgLog 查询条件
     * @param page   页码
     * @param size   页大小
     * @return 分页结果
     */
    @Override
    public BaseResponse findMsgLogList(@RequestBody MsgLog msgLog, @PathVariable(value = "page") int page, @PathVariable(value = "size") int size) {
        //分页
        PageHelper.startPage(page, size);

        PageInfo<MsgLog> pagelist = new PageInfo<MsgLog>(msgLogMapper.select(msgLog));

        return setResultSuccess(pagelist);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Override
    public BaseResponse deleteMsgLogById(@PathVariable(value = "ids") String ids) {

        String[] split = ids.split(",");
        Example example = new Example(MsgLog.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(split));
        msgLogMapper.deleteByExample(example);
        return setResultSuccess("删除成功");
    }

    /**
     * 修改MsgLog
     *
     * @param msgLog
     */
    @Override
    public BaseResponse updateMsgLog(@RequestBody MsgLog msgLog) {
        msgLogMapper.updateByPrimaryKeySelective(msgLog);
        return setResultSuccess("修改成功");
    }

    /**
     * 增加MsgLog
     *
     * @param msgLog
     */
    @Override
    public BaseResponse addMsgLog(@RequestBody MsgLog msgLog) {
        msgLogMapper.insertSelective(msgLog);
        return setResultSuccess("添加成功");
    }

    /**
     * 根据ID查询MsgLog
     *
     * @param id
     * @return
     */
    @Override
    public BaseResponse findMsgLogById(@PathVariable(value = "id") String id) {
        return setResultSuccess(msgLogMapper.selectByPrimaryKey(id));
    }

    /**
     * 查询MsgLog全部数据
     *
     * @return
     */
    @Override
    public BaseResponse findMsgLogAll() {
        return setResultSuccess(msgLogMapper.selectAll());
    }
}
